package com.yesep.netty;

import com.yesep.model.ChannelModel;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: gateway
 * @description: 管道管理
 * @author: quliang
 * @create: 2019-07-04 12:23
 **/
public class NettyChannelManager {

    //存放channelId与channel的关系
    private final Map<String, Channel> activeChannelMap = new ConcurrentHashMap<String, Channel>(400);

    //存放MN与channelId的关系
    private final Map<String, String> mnChannelMap = new ConcurrentHashMap<String, String>(400);

    public static NettyChannelManager getInstance() {
        return NettyChannelManagerHolder.instance;
    }


    public static class NettyChannelManagerHolder {
        private static NettyChannelManager instance = new NettyChannelManager();

        public NettyChannelManagerHolder() {
        }
    }


    /**
     * 通过设备唯一编号(mn)获取管道(channel)
     *
     * @param mn
     * @return
     */
    public Channel getChannelByMn(String mn) {
        if (mn == null || "".equals(mn)) {
            return null;
        }
        String channelId = this.mnChannelMap.get(mn);
        if (channelId == null || "".equals(channelId)) {
            return null;
        }
//        System.out.println(this.activeChannelMap.size());
        System.out.println("activeChannelMap = " + activeChannelMap.size());
        return this.activeChannelMap.get(channelId);
    }


    /**
     * 绑定设备唯一编号(mn)和管道编号(channelId)
     *
     * @param mn
     * @param channelId
     */
    public void bindMnAndChannel(String mn, String channelId) {
        if (mn == null || "".equals(mn)) {
            return;
        }
        if (channelId == null || "".equals(channelId)) {
            return;
        }
        this.mnChannelMap.put(mn, channelId);
        System.out.println("mnChannelMap = " + mnChannelMap.size());
    }


    /**
     * 添加channel
     *
     * @param channel
     */
    public void putChannel(Channel channel) {
        String channelId = channel.id().asLongText();
        this.activeChannelMap.putIfAbsent(channelId, channel);
//        this.mnChannelMap.putIfAbsent(channel.remoteAddress().toString(), channelId);
        System.out.println("activeChannelMap = " + activeChannelMap.size());
        System.out.println("activeChannelMap.toString()=" + activeChannelMap.toString());
    }

    /**
     * 删除channel
     *
     * @param channel
     */
    public void removeChannel(Channel channel) {
        if (channel == null) {
            return;
        }

        String channelId = channel.id().asLongText();
        Channel channelOld = null;

        if (!Objects.equals(mnChannelMap, null) && mnChannelMap.size() > 0) {
            for (String mn : mnChannelMap.keySet()) {
                if (mnChannelMap.get(mn).equals(channelId)) {
                    channelOld = this.activeChannelMap.remove(channelId);
                    this.mnChannelMap.remove(mn);
                }
            }
        }
        System.out.println("activeChannelMap" + activeChannelMap.size());
        System.out.println("mnChannelMap" + mnChannelMap.size());
        if (channelOld == null) {
            return;
        }
        channel.close();
    }
}
